from ast import alias
from pathlib import Path
from unittest import result
from fastapi import APIRouter, Request, Query, Path
from typing import Optional, List
from pydantic import BaseModel
from enum import Enum

testRouter = APIRouter(prefix="/test", tags=['测试'])


class ModelName(str, Enum):
    alexnet = "alexnet",
    resnet = "resnet",
    lenet = 'lenet'


class Item(BaseModel):
    name: str
    diescription: Optional[str] = None
    price: float
    tax: Optional[float] = None


@testRouter.get('/', summary="根路径")
async def read_root():
    return {"Hello": "World"}


@testRouter.get("/items/{item_id}", summary="路径参数与默认值约束")
async def read_items(item_id: int, q: Optional[str] = Query(None, min_length=3, max_length=50, regex="^fixedquery$")):
    """
    Query(None)  默认值为None
    Query(...)   无默认值
    min_length   最小长度
    max_length   最大长度
    regex        正则表达式
    查询多个值
    """
    result = {"items": [{"item_id": "Foo"}, {
        "item_id": "Bar"}, {'item_id': item_id}]}
    if q:
        result.update({'q': q})

    return result


@testRouter.get('/coordList')
async def get_coord_list(q: Optional[List[str]] = Query(None, title="Query string")):
    query_items = {"q": q}
    return query_items


@testRouter.post("/items/", summary="创建items 由Item类创建")
async def creta_item(item: Item):
    return item


@testRouter.put('/items/{item_id}', summary="向item中添加元素")
async def create_item(item_id: int, item: Item):
    return {'item_id': item_id, **item.dict()}


@testRouter.get("/perosn/{person_id}",summary="使用别名接受入参")
async def read_person(person_id: int = Path(..., title="The ID of the person"), q: Optional[str] = Query(None, alias="person-qurey")):
    result = {"person_id": person_id, 'q': q}
    return result


@testRouter.get("/user/me", summary="获取当前用户信息")
async def read_user_me():
    return {"user_id": "this current user"}


@testRouter.get("/user/{user_id}", summary="查询用户id")
async def read_user(user_id: str):
    return {"user_id": user_id}


@testRouter.get("/models/{modul_name}", summary="查询模块名称及message")
async def get_modules(modul_name: ModelName):
    if modul_name == ModelName.alexnet:
        return {"modul_name": modul_name}
    if modul_name.value == "lenet":
        return {"model_name": modul_name, "message": "LeCNN all the images"}
    return {"model_name": modul_name, "message": "Have some residuals"}


fake_items_db = [{"item_name": "Foo"}, {
    "item_name": "Bar"}, {"item_name": "Baz"}]


@testRouter.get("/fakeitems/", summary="参数查询，模拟分页数据")
async def read_fakeitems(skip: int = 0, limit: int = 10):
    return fake_items_db[skip: skip + limit]
